<template>
  <div class="home-container">
    <!-- 顶部搜索按钮 -->
    <van-nav-bar fixed>
      <van-button
        slot="title"
        class="search-btn"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
      >
        搜索
      </van-button>
    </van-nav-bar>

    <!-- 频道标签列表 -->
    <van-tabs swipeable animated v-model="active" class="channel-tabs">
      <!-- 循环生成 频道标签 -->
      <van-tab v-for="ch in channelList" :key="ch.id" :title="ch.name">
        <ArtList :chan="ch" />
      </van-tab>

      <!-- 汉堡按钮 -->
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="isShowEdit = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>

    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isShowEdit"
      position="bottom"
      closeable
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <ChannelEdit
        @onChangeChannel="changeChan"
        :usrChannList="channelList"
        :myIndex="active"
      />
    </van-popup>
  </div>
</template>

<script>
// 导入组件
import ArtList from './components/ArtList.vue'
import ChannelEdit from './components/ChannelEdit.vue'
// 导入 接口函数
import { getUserChannels } from '@/api/channels.js'
// 导入 getItem
import { getItem } from '@/utils/storage.js'
// 导入 配置信息对象
import config from '@/config/index.js'

export default {
  name: 'home',
  props: {},
  data() {
    return {
      isShowEdit: false,
      active: 0,
      channelList: []
    }
  },
  // 钩子函数（vue组件的入口函数）
  async created() {
    // 1.先请求 服务器 获取 “我的频道” 列表
    // a.如果 有登录，则 返回 登录用户的频道
    // b.如果 未登录，则 返回 推荐频道列表
    const res = await getUserChannels()
    let channels = res.data.data.channels

    // 2.如果未登录,则从本地获取
    if (!this.$store.state.user) {
      // a.获取本地 频道列表
      const localUsrChans = getItem(config.USER_CHAN)
      // b.如果本地频道列表有，则在当前未登录情况下，优先使用本地列表
      if (localUsrChans) {
        // c.将 本地列表 覆盖给 channles 变量
        channels = localUsrChans
      }
    }

    // 3.不管 登录/未登录，都将 频道列表 设置给 data.channelList 渲染到视图中
    this.channelList = channels
    // console.log(data)
  },
  methods: {
    // 1.修改频道---------
    changeChan(i, isShowEdit = true) {
      console.log(i)
      // 根据下标 切换 频道
      this.active = i
      // 关闭 编辑弹出层
      this.isShowEdit = isShowEdit
    }
  },
  components: {
    ArtList,
    ChannelEdit
  }
}
</script>

<style lang="less" scoped>
.home-container {
  // 1.标题栏---------------------------------
  /deep/ .van-nav-bar__title {
    max-width: unset !important;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
      color: #fff;
    }
  }
  // 2.tab栏 ---------------------------------
  /deep/ .channel-tabs {
    padding-top: 180px;
    padding-bottom: 100px;
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777;
    }

    .van-tab--active {
      color: #333;
    }
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
  }
  // 3.汉堡按钮 ---------------------------------
  // 3.1 汉堡按钮的占位符
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
  // 3.2 汉堡按钮样式
  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    background-color: rgba(255, 255, 255, 0.902);
    i.toutiao {
      font-size: 33px;
    }
    &:before {
      content: '';
      position: absolute;
      left: 0;
      width: 1px;
      height: 58px;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>
